package com.mysteel.cyx.demo.face.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.metadata.data.CellData;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import com.alibaba.excel.write.style.column.AbstractColumnWidthStyleStrategy;
import com.google.common.base.Throwables;
import com.mysteel.cyx.demo.biz.export.CustomCellWriteHandler;
import com.mysteel.cyx.demo.biz.export.CustomRowHeightHandler;
import com.mysteel.cyx.demo.biz.export.bo.ComplianceMonitoringDailyB0;
import com.mysteel.cyx.demo.biz.export.complianceMonitoringDailyReport;
import com.mysteel.cyx.demo.service.ComplianceMonitoringDailyReportService;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.List;

/**
 * @author Vicky(陈烟星)
 * @date 2024/6/18 13:51
 */
@RestController
@Slf4j
public class ComplianceMonitoringDailyReportController {


    @Autowired
    private ComplianceMonitoringDailyReportService complianceMonitoringDailyReportService;


    @GetMapping("/export/compliance/monitoring/daily/report")
    public void export(HttpServletResponse response) throws IOException {
        String fileName = "监控日报.xlsx";

        // 设置响应头，使得浏览器能够识别文件类型并提示下载
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding(StandardCharsets.UTF_8.name());
        // 防止中文乱码
        fileName = URLEncoder.encode(fileName, String.valueOf(StandardCharsets.UTF_8)).replaceAll("\\+", "%20");
        response.setHeader("Content-Disposition", "attachment;filename*=utf-8''" + fileName);

        //数据准备
        ComplianceMonitoringDailyB0 complianceMonitoringDailyB0 = complianceMonitoringDailyReportService.dataPrepataion();


        ExcelWriter excelWriter = null;
        try {
            excelWriter = EasyExcel.write(response.getOutputStream())
                    .registerWriteHandler(new CustomRowHeightHandler())
                    .registerWriteHandler(getstylestrategyg())
                    .registerWriteHandler(new CustomCellWriteHandler(complianceMonitoringDailyB0.getBusinessDate()))
                    .build();
            complianceMonitoringDailyReport complianceMonitoringDailyReport = new complianceMonitoringDailyReport();
            complianceMonitoringDailyReport.exportComplianceMonitoringDailyReport(excelWriter, complianceMonitoringDailyB0);
        } catch (Throwable e) {
            log.error("export error:{}", Throwables.getStackTraceAsString(e));
        } finally {
            // 关闭ExcelWriter
            if (excelWriter != null) {
                excelWriter.finish();
            }
        }

    }


    private HorizontalCellStyleStrategy getstylestrategyg() {
//定义单元格样式策略
        WriteCellStyle headcellStyle = new WriteCellStyle();
        headcellStyle.setBorderTop(BorderStyle.NONE);
        headcellStyle.setBorderBottom(BorderStyle.NONE);
        headcellStyle.setBorderLeft(BorderStyle.NONE);
        headcellStyle.setBorderRight(BorderStyle.NONE);

        WriteCellStyle contentCellStyle = new WriteCellStyle();
        contentCellStyle.setBorderTop(BorderStyle.THIN);
        contentCellStyle.setBorderBottom(BorderStyle.THIN);
        contentCellStyle.setBorderLeft(BorderStyle.THIN);
        contentCellStyle.setBorderRight(BorderStyle.THIN);
        contentCellStyle.setWrapped(true);
        return new HorizontalCellStyleStrategy(headcellStyle, contentCellStyle);
    }
}
